package com.ly.lypopupwindowdemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.fragment.MovieFragment;
import com.ly.lypopupwindowdemo.fragment.MusicFragment;
import com.ly.lypopupwindowdemo.fragment.NotiicationFragment;
import com.ly.lypopupwindowdemo.fragment.TakePhotoFragment;

import java.util.ArrayList;
import java.util.List;

public class MultimediaActivity extends AppCompatActivity {

    public Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multimedia);

        context = this;
        initToolbar();

        Fragment demoFragment = Fragment.instantiate(this, MultimediaListFragment.class.getName());
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, demoFragment);
        fragmentTransaction.commit();

        getSupportFragmentManager().addOnBackStackChangedListener(
                new FragmentManager.OnBackStackChangedListener() {
                    @Override
                    public void onBackStackChanged() {
                        int count = getSupportFragmentManager().getBackStackEntryCount();
                        ActionBar actionbar = getSupportActionBar();
                        if (actionbar != null) {
                            actionbar.setDisplayHomeAsUpEnabled(count > 0);
                            actionbar.setDisplayShowHomeEnabled(count > 0);
                        }
                    }
                });
    }

    private void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public static class MultimediaListFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return new RecyclerView(getContext());

        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

            List<SampleInfo> mList = new ArrayList<>();

            mList.add(new SampleInfo("Android通知", NotiicationFragment.class.getName()));
            mList.add(new SampleInfo("Android相机", TakePhotoFragment.class.getName()));
            mList.add(new SampleInfo("Android播放音频", MusicFragment.class.getName()));
            mList.add(new SampleInfo("Android播放视频", MovieFragment.class.getName()));


            MultimediaAdapter adapter = new MultimediaAdapter(mList);
            RecyclerView recycleView = (RecyclerView) view;
            recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
            recycleView.setAdapter(adapter);
        }

        private class MultimediaAdapter extends RecyclerView.Adapter<MultimediaAdapter.MyViewHolder> {

            private List<SampleInfo> mList;

            public MultimediaAdapter(List<SampleInfo> mList) {
                this.mList = mList;
            }

            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.mutimedia_item, parent, false);
                MyViewHolder holder = new MyViewHolder(view);
                view.setTag(holder);
                return holder;
            }

            @Override
            public void onBindViewHolder(final MyViewHolder holder, int position) {

                final SampleInfo bean = mList.get(position);
                holder.title_tv.setText(bean.title);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        navigateToFragment(bean.getFragmentName());
                    }
                });
            }

            private void navigateToFragment(String fragmentName) {

                Fragment fragment = Fragment.instantiate(getContext(), fragmentName);

                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
                        android.R.anim.fade_in, android.R.anim.fade_out);
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.addToBackStack(fragmentName);
                fragmentTransaction.commit();

            }

            @Override
            public int getItemCount() {
                return mList.size();
            }

            public class MyViewHolder extends RecyclerView.ViewHolder {

                private TextView title_tv;

                public MyViewHolder(View itemView) {
                    super(itemView);
                    title_tv = (TextView) itemView.findViewById(R.id.title);
                }
            }
        }

        private class SampleInfo {
            public String title;
            public String fragmentName;

            public SampleInfo(String title, String fragmentName) {
                this.title = title;
                this.fragmentName = fragmentName;
            }

            public String getTitle() {
                return title;
            }

            public void setTitle(String title) {
                this.title = title;
            }

            public String getFragmentName() {
                return fragmentName;
            }

            public void setFragmentName(String fragmentName) {
                this.fragmentName = fragmentName;
            }
        }


    }


}
